<?php

class News_UI_GroupLoader
{
    var $Controller;
    function News_UI_GroupLoader($controller)
    {
        $this->Controller = $controller;
    }
    
    function Display ()
    {
        $name    = $this->Controller->Settings['group'];
        $index   = $this->Controller->Settings['index'];
        $item    = $this->Controller->Settings['item'];
        $more    = $this->Controller->Settings['more'];
        $find    = $this->Controller->Settings['find'];
        $page    = $this->Controller->Settings['page'];
        $request = $this->Controller->Settings['request'];
        if ($request=="download")
        {
            $this->Controller->Save("notify/{$name}.{$item}.{$index}.index", $name);   
            echo "OK";
            return;
        }
        else if ($request=="remove")
        {
            $this->Controller->Save("notify/{$name}.{$index}.remove", $name);     
        }
        else if ($request=="find")
        {
            $this->Controller->Save("notify/{$name}.{$find}.find", $name);     
        }
         else if ($request=="status")
        {
            if (!file_exists("notify/{$name}.{$item}.{$index}.index"))
            {
                echo "OK";
            }
            return;
        }
        else if ($request=="get")
        {
            $this->Controller->Save("notify/{$name}.{$index}.get", $name);   
        }
        else if (strlen($index) > 0)
        {
            $notifier = "notify/{$name}.{$index}.index";
            $this->Controller->Save($notifier, "{$name}?{$item}");   
        }
        else if (strlen($page) > 0)
        {
            $this->Controller->Save("notify/{$name}.{$page}.page", $name);   
        }
        else
        {
            $message = $name;
            if ($request == 'renew')
            {
                 $oldname = "data/{$name}.xml";
                 if (strlen($more) > 0)
                 {
                     for ($fn=1;$fn<100;$fn++)
                     {
                         $newname = "data/{$name},{$fn}.xml";
                         if (!file_exists($newname))
                         {
                             rename ($oldname, $newname);
                             break;
                         }
                     } 
                     $message .= "|{$more}";
                 }
            } 
            if ($request == 'restore')
            {
                 $newname = "data/{$name},{$more}.xml";  
                 $oldname = "data/{$name}.xml";
                 if (file_exists($newname))
                 {
                      rename ($oldname, $oldname.'.tmp');
                      rename ($newname, $oldname);
                      rename ($oldname.'.tmp', $newname);
                      redirect ("/ui/articlelist/group/{$name}/page/1");
                 }
            } 
            $this->Controller->Save("notify/{$name}.group", $message);
        }
        
        ?>
        <html>
            <head>
            <title>SuperNET</title>
            <script language="javascript" src="http://www.101stratford.com/101/ebp6/js/xmlextras.js"></script>
            <script language="javascript">
            var timer = 0;
            function load()
            {
                <?php echo "var name = '{$name}';\n";  ?>
                <?php echo "var page = '{$page}';\n";  ?>
                <?php echo "var find = '{$find}';\n";  ?>
                <?php echo "var index = '{$index}';\n";  ?>
                <?php echo "var item = '{$item}';\n";  ?>
                <?php echo "var req = '{$request}';\n";  ?>

                var uri = "/index.php?status=" + name + "&page=" + page + "&index=" + index + "&find=" + find + "&request=" + req;
	            var url = page.length > 0 
                        ? ("/ui/articlelist/group/" + name + "/page/" + page)
		                : ("/ui/grouploader/group/" + name + "/page/1");
		        if (index.length > 0)
		        {
			        url += "/index/" + index + "/item/" + item;
		        }
		        if (find.length > 0)
		        {
			        url += "/find/" + find;
		        }
		        if (req == 'remove') url = '/ui/bookmarks/page/' + page;
		         
	            var mhttp=XmlHttp.create();                                                                                                                                                                                                                                                                   
	            mhttp.open("GET", uri , true);      
                
	            mhttp.onreadystatechange = function ()                                              
	            {                                              
	                if (mhttp.readyState == 4)                                              
	                {           
		                var result = mhttp.responseText; 
	    	            if (result=="OK") 
	    	            {
	    	                location.href = url;
	    	                return;
	    	            } 
	    	            timer ++;
	    	            var blank = "http://www.101stratford.com/webservices/images/clear.gif";
	    	            var image = "/data/progress.jpeg";
	    	            $('pre').onerror = function () { this.src = blank; }
	    	            $('pre').onload  = function () { this.src = image + "?" + timer; }
	    	            $('pre').src = image + "?" + timer;
//	    	            $('stat').innerHTML = line;
	                    setTimeout(load, 1500);                    
	                }  
	            }                
	            mhttp.send();     
	     
    
            }
            function $(i) { return document.getElementById(i); };
            </script>
            </head>
            <body onload="load()">
                <table width="100%" height="100%">
                    <tr>
                        <td align="center"> 
                            <img src="http://www.101stratford.com/webservices/images/clear.gif" id="pre"/>
                        </td>
                    </tr>
                </table> 
            </body>
        </html>
        
        
        <?php
    }
} 